package com.useeinfo.oa.modules.base.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.Resource;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 陈炜
 * @Date: 2018/12/11 16:55
 * @Description: 资源管理dao
 */
@Repository("resourceDao")
public class ResourceDao implements CrudDao<Resource> {
    private final static Logger logger = LoggerFactory.getLogger(ResourceDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash){
        Map<String, Object> conditionHash = new HashMap<>();

        conditionHash.put("re.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }

        String resourceId = queryHash.get("resourceId");
        if (!StringUtils.isEmpty(resourceId)) {
            conditionHash.put("(re.id = ?{paramIndex}) ", StringConverters.ToLong(resourceId));
        }


        String enterpriseId = queryHash.get("enterpriseId");
        if (!StringUtils.isEmpty(enterpriseId)) {
            conditionHash.put("(re.enterprise.id = ?{paramIndex}) ", StringConverters.ToLong(enterpriseId));
        }
        return conditionHash;
    }

    /**
     * 获取总记录数
     */
    @Override
    public Long totalRecord(Map<String, String> queryHash){
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(re) from Resource re ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    /**
     * 分页列表
     */
    @Override
    public List<Resource> findList(QueryParam queryParam){
        String sqlInfo = queryParam.joinJPQL("select re from Resource re ", "order by re.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<Resource> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, Resource.class);
        return queryParam.findPageList(typedQuery);
    }

    /**
     * id获取记录
     */
    @Override
    public Resource findModel(Long id){
        return entityManager.find(Resource.class, id);
    }

    /**
     * 增加记录
     */
    @Override
    public Integer add(Resource resource){
        resource.setCreateDate(new Date());
        entityManager.persist(resource);
        logger.info("ResourceDaoImpl添加resource成功！");
        return 1;
    }

    /**
     * 修改记录
     */
    @Override
    public Integer update(Resource resource){
        resource.setUpdateDate(new Date());
        entityManager.merge(resource);
        return 1;
    }

    /**
     * 删除记录
     */
    @Override
    public Integer delete(Long id){
        Resource resource = entityManager.find(Resource.class, id);
        entityManager.remove(resource);
        return 1;
    }

    /**
     * 删除记录 - 逻辑删除
     */
    @Override
    public Integer deleteLogic(Resource resource){
        resource.setDeleteStatus(true);
        return 1;
    }
}
